<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="父级" prop="parentId">
        <el-select v-model="queryParams.parentId" placeholder="请选择父级" clearable size="small">
          <el-option  v-for="item in selectTypeList" :key="item.infoTypeId" 
          :label="item.typeName" :value="item.infoTypeId" v-show="item.parentId==0" />
        </el-select>
      </el-form-item>
      <el-form-item label="题型名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入题型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['surveys:type:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="typeList"
      row-key="infoTypeId"
      
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <!-- <el-table-column label="父级" align="center" prop="parentId" visable="false"  /> -->
      <el-table-column label="题型名称" align="center" prop="typeName" />
      <el-table-column label="描述说明" align="center" prop="description" />
      <el-table-column label="排序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['surveys:type:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['surveys:type:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改问卷类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父级" prop="parentId">
          <treeselect v-model="form.parentId" :options="typeOptions" :normalizer="normalizer" placeholder="请选择父级" />
        </el-form-item>
        <el-form-item label="题型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入题型名称" />
        </el-form-item>
        <el-form-item label="描述说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述说明" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, exportType } from "@/api/surveys/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Type",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 问卷类型表格数据
      typeList: [],
      // 搜索问卷类型表格数据
      selectTypeList: [],
      // 问卷类型树选项
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        parentId: undefined,
        typeName: undefined,
        orderByColumn:'orderNum',
        isAsc:'asc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
         typeName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSelectTypeList();
  },
  methods: {
    /** 查询问卷类型列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = this.handleTree(response.data, "infoTypeId", "parentId");
        this.loading = false;
      });
    },
     /** 查询问卷类型列表 */
    getSelectTypeList() {
      var param={
        parentId:0
      };
      listType(param).then(response => {
        this.selectTypeList = this.handleTree(response.data, "infoTypeId", "parentId");
      });
    },
    /** 转换问卷类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.infoTypeId,
        label: node.typeName,
        children: node.children
      };
    },
	/** 查询部门下拉树结构 */
    getTreeselect() {
      listType().then(response => {
        this.typeOptions = [];
        const data = { infoTypeId: 0, typeName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "infoTypeId", "parentId");
        this.typeOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        infoTypeId: undefined,
        parentId: undefined,
        typeName: undefined,
        description: undefined,
        orderNum: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
	  this.getTreeselect();
      this.open = true;
      this.title = "添加问卷类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.getTreeselect();
      if (row != undefined) {
        this.form.parentId = row.infoTypeId;
      }
      getType(row.infoTypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问卷类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.infoTypeId != undefined) {
            updateType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除问卷类型编号为"' + row.infoTypeId + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delType(row.infoTypeId);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>